﻿using Newtonsoft.Json;
using System.ComponentModel;


public interface IResultDTO
{
    [Description("消息")]
    string Message { get; set; }

    [Description("编码")]
    int Code { get; set; }

    [Description("是否成功")]
    bool Success { get; set; }
}

public class ResultDTO : IResultDTO
{
    [Description("消息")]
    public string Message { get; set; } = "";
    [Description("编码")]
    public int Code { get; set; }
    [Description("是否成功")]
    public bool Success { get; set; }

    #region Ok
    public static IResultDTO Ok()
    {
        return new ResultDTO() { Success = true, Code = 200, Message = "操作成功" };
    }
    public static IResultDTO Ok(int code)
    {
        return new ResultDTO() { Success = true, Code = code, Message = "操作成功" };
    }

    public static IResultDTO Ok(string message)
    {
        return new ResultDTO() { Success = true, Code = 200, Message = message };
    }

    public static IResultDTO Ok(int code, string message)
    {
        return new ResultDTO() { Success = true, Code = code, Message = message };
    }
    #endregion

    #region OK
    public static IResultDTO<T> Ok<T>(T t)
    {
        return new ResultDTO<T>() { Data = t, Success = true, Code = 200, Message = "操作成功" };
    }

    public static IResultDTO<T> Ok<T>(T t, int code)
    {
        return new ResultDTO<T>() { Data = t, Success = true, Code = code, Message = "操作成功" };
    }

    public static IResultDTO<T> Ok<T>(T t, string message)
    {
        return new ResultDTO<T>() { Data = t, Success = true, Code = 200, Message = message };
    }

    public static IResultDTO<T> Ok<T>(T t, int code, string message)
    {
        return new ResultDTO<T>() { Data = t, Success = true, Code = code, Message = message };
    }
    #endregion

    #region Error
    public static IResultDTO Error()
    {
        return new ResultDTO() { Success = false, Code = 500, Message = "操作失败" };
    }
    public static IResultDTO Error(int code)
    {
        return new ResultDTO() { Success = false, Code = code, Message = "操作失败" };
    }

    public static IResultDTO Error(string message)
    {
        return new ResultDTO() { Success = false, Code = 500, Message = message };
    }

    public static IResultDTO Error(int code, string message)
    {
        return new ResultDTO() { Success = false, Code = code, Message = message };
    }


    #endregion
    #region MyRegion
    public static IResultDTO<T> Error<T>()
    {
        return new ResultDTO<T>() { Success = false, Code = 500, Message = "操作失败" };
    }
    public static IResultDTO<T> Error<T>(int code)
    {
        return new ResultDTO<T>() { Success = false, Code = code, Message = "操作失败" };
    }

    public static IResultDTO<T> Error<T>(string message)
    {
        return new ResultDTO<T>() { Success = false, Code = 500, Message = message };
    }

    public static IResultDTO<T> Error<T>(int code, string message)
    {
        return new ResultDTO<T>() { Success = false, Code = code, Message = message };
    }
    public static IResultDTO Result(bool v)
    {
        return new ResultDTO() { Success = v, Code = v ? 200 : -1, Message = v ? "操作成功" : "操作失败" };
    }
    public static IResultDTO<T> Result<T>(bool v, T t)
    {
        return new ResultDTO<T>() { Success = v, Code = v ? 200 : -1, Data=t, Message = v ? "操作成功" : "操作失败" };
    }
    #endregion
}


public interface IResultDTO<T> : IResultDTO
{
    public T? Data { get; set; }
}

public class ResultDTO<T> : ResultDTO, IResultDTO<T>
{
    public T? Data { get; set; }
}



#region page


public class PageOutput
{
    /// <summary>
    /// 
    /// </summary>
    public PageOutput()
    {

    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="pageSize"></param>
    /// <param name="currentPage"></param>
    public PageOutput(int pageSize, int currentPage)
    {
        this.PageSize = pageSize;
        this.CurrentPage = currentPage;
    }

    /// <summary>
    /// 页面大小
    /// </summary>
    public int PageSize { get; set; } = 0;

    /// <summary>
    /// 当前页
    /// </summary>
    public int CurrentPage { get; set; } = 0;


    /// <summary>
    /// 总页数
    /// </summary>
    public int TotalPage
    {
        get
        {
            return (int)Math.Ceiling((double)Total / PageSize);
        }
    }
    /// <summary>
    /// 数据总数
    /// </summary>
    public long Total { get; set; } = 0;

    /// <summary>
    /// 数据列表
    /// </summary>
    public object? List { get; set; }

    /// <summary>
    /// 扩展信息
    /// </summary>
    public dynamic? Extend { get; set; }

    /// <summary>
    /// 统计信息
    /// </summary>
    public object? Summary { get; set; }
}

/// <summary>
/// 
/// </summary>
/// <typeparam name="T"></typeparam>
public class PageOutput<T>
{
    /// <summary>
    /// 
    /// </summary>
    public PageOutput()
    {

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="pageSize"></param>
    /// <param name="currentPage"></param>
    public PageOutput(int pageSize, int currentPage)
    {
        this.PageSize = pageSize;
        this.CurrentPage = currentPage;
    }

    /// <summary>
    /// 页面大小
    /// </summary>
    public int PageSize { get; set; } = 0;

    /// <summary>
    /// 当前页
    /// </summary>
    public int CurrentPage { get; set; } = 0;


    /// <summary>
    /// 总页数
    /// </summary>
    public int TotalPage
    {
        get
        {
            return (int)Math.Ceiling((double)Total / PageSize);
        }
    }
    /// <summary>
    /// 数据总数
    /// </summary>
    public long Total { get; set; } = 0;

    /// <summary>
    /// 数据列表
    /// </summary>
    public IList<T>? List { get; set; }

    /// <summary>
    /// 扩展信息
    /// </summary>
    public dynamic? Extend { get; set; }

    /// <summary>
    /// 统计信息
    /// </summary>
    public object? Summary { get; set; }
}


/// <summary>
/// 
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="S"></typeparam>
public class PageOutput<T, S> : PageOutput<T>
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="pageSize"></param>
    /// <param name="currentPage"></param>
    public PageOutput(int pageSize, int currentPage) : base(pageSize, currentPage)
    {

    }


    /// <summary>
    /// 扩展
    /// </summary>
    public new S? Extend { get; set; }
}
#endregion

#region pageInput

public class OderInput
{
    /// <summary>
    /// 排序属性字段
    /// </summary>
    [DefaultValue("")]
    public string? Sort { get; set; }

    /// <summary>
    /// 排序 是否升序
    /// </summary>
    [DefaultValue(true)]
    public bool IsAsc
    {
        get
        {
            return "ascending" == Order;
        }
    }

    /// <summary>
    /// 排序方向 ascending、descending
    /// </summary>
    [DefaultValue("")]
    public string? Order { get; set; } = "";
}


/// <summary>
/// 
/// </summary>
/// <typeparam name="T"></typeparam>
public class FilterPara<T> : OderInput
{
    /// <summary>
    /// 查询条件
    /// </summary>
    public T? Filter { get; set; }

  

}
/// <summary>
/// 
/// </summary>
public class PageInput
{
    private int currentPage = 1;

    /// <summary>
    /// 当前页标
    /// </summary>
    [DefaultValue(1)]
    public int CurrentPage
    {
        get
        {
            if (this.currentPage < 1)
            {
                return 1;
            }
            return this.currentPage;
        }
        set
        {
            this.currentPage = value;
        }
    }


    private int pageSize = 50;
    /// <summary>
    /// 每页大小
    /// </summary>
    [DefaultValue(50)]
    public int PageSize
    {
        get
        {
            if (this.pageSize < 1)
            {
                return 1;
            }
            return this.pageSize;
        }
        set
        {
            this.pageSize = value;
        }
    }
}
/// <summary>
/// 
/// </summary>
/// <typeparam name="T"></typeparam>
public class PagePara<T> : FilterPara<T>
{
    private int currentPage = 1;

    /// <summary>
    /// 当前页标
    /// </summary>
    [DefaultValue(1)]
    public int CurrentPage
    {
        get
        {
            if (this.currentPage < 1)
            {
                return 1;
            }
            return this.currentPage;
        }
        set
        {
            this.currentPage = value;
        }
    }


    private int pageSize = 50;
    /// <summary>
    /// 每页大小
    /// </summary>
    [DefaultValue(50)]
    public int PageSize
    {
        get
        {
            if (this.pageSize < 1)
            {
                return 1;
            }
            return this.pageSize;
        }
        set
        {
            this.pageSize = value;
        }
    }

    private int skip = 0;
    /// <summary>
    /// 跳过数
    /// </summary>
    [JsonIgnore]
    public int Skip
    {
        get
        {
            if (this.skip != 0)
            {
                return skip;
            }
            else
            {
                return (this.CurrentPage - 1) * this.PageSize;
            }
        }
        set
        {
            this.skip = value;
        }
    }

    /// <summary>
    /// 是否输出记录数
    /// </summary>
    [JsonIgnore]
    public bool Count { get; set; } = true;

}






#endregion


